import { GrpcMethod } from '@nestjs/microservices'
import { Controller, Get, Injectable, Inject } from '@nestjs/common';
import { School } from 'src/models/school.model';
import { SchoolReqMessage } from './types.d/school.request.message';
import { SchoolRespMessage } from './types.d/school.response.message';
import { SchoolService } from './types.d/school.service';
import { ListSchoolRespMessage } from './types.d/listschool.response.message';
import { SchoolDaoService } from 'src/dao/school.dao';
import { CreateSchoolReqMessage } from './types.d/createschool.response.message';



@Controller()
export class SchoolRPCService implements SchoolService{
  constructor( private dao: SchoolDaoService){}

  @GrpcMethod('SchoolRPCService', 'QueryListSchool')
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  async queryListSchool(data: any): Promise<ListSchoolRespMessage> {
    console.log(data)
    const result:Array<School> = await this.dao.querySchoolList()
    return {
      code: 200,
      msg: "获取成功",
      data: result || []
    };
  }

  @GrpcMethod('SchoolRPCService', 'DeleteOneSchool')
  async deleteOneSchool(data: SchoolReqMessage): Promise<SchoolRespMessage >{
    const result:School = await this.dao.queryOneSchool(data.id);
    this.dao.deleteOneSchool(data.id);
    return {
      code: 200,
      msg: "删除成功",
      data: result 
    };
  }


  @GrpcMethod('SchoolRPCService', 'CreateOneSchool')
  async createOneSchool(data: CreateSchoolReqMessage): Promise<SchoolRespMessage >{
    const result: School = await this.dao.createOneSchool(data)
    return {
      code: 200,
      msg: "新建成功",
      data:result
    };
  }

  @GrpcMethod('SchoolRPCService', 'PatchOneSchool')
  async patchOneSchool(data: School): Promise<SchoolRespMessage >{
    await this.dao.patchOneSchool(data);
    return {
      code: 200,
      msg: "修改成功",
      data
    };
  }


  @GrpcMethod('SchoolRPCService', 'QueryOneSchool')
  async queryOneSchool(data: SchoolReqMessage): Promise<SchoolRespMessage>{
    const result:School = await this.dao.queryOneSchool(data.id);
    return {
      code: 200,
      msg: "获取成功",
      data: result
    };
  }
}